const app = getApp()

Page({
  data: {
    level: 1,
    pageTop: '首先请静下来，看清楚测试题目要求。真实的说出当时脑子里第一个答案，这样你才能真的体会你的智慧。',
    isTrue: '',
    isError: '',
    isOver: false,
    rightNumber: 0,
    quesArr: [],
  },

  onLoad: function () {
    this.getQuestion()
  },

  // 选择完选项，显示正确答案
  chooseAnswer: function (e) {
    var that = this
    if (that.data.isTrue == '' && that.data.isError == '') {
      var choseAnswer = e.currentTarget.dataset.radio
      if (choseAnswer != that.data.answer) {
        that.setData({
          isError: choseAnswer
        })
      }
      that.setData({
        isTrue: that.data.answer
      })

      if (choseAnswer == that.data.answer) {
        that.setData({
          rightNumber: that.data.rightNumber + 1
        })
      }
      if (that.data.level == 10) {
        that.setData({
          isOver: true
        })
      }
    }
  },

  // 下一题
  nextQuestion: function () {
    var that = this
    if (that.data.isTrue == '') {
      wx.showToast({
        title: '莫着急，请先答完这题',
        icon: 'none',
        duration: 1500,
      });
      return
    }
    if (!that.data.isOver) {
      that.setQuestion(that.data.level)
      this.setData({
        isTrue: '',
        isError: '',
        isChecked: false,
        level: that.data.level + 1
      })
    } else {
      // 跳转结果页
      wx.navigateTo({
        url: "/pages/result/result?rightNum=" + that.data.rightNumber
      });
    }

  },

  // 获取题目
  getQuestion: function () {
    var that = this
    wx.request({
      url: app.globalData.url + 'get_questions',
      success: function (res) {
        that.setData({
          quesArr: res.data.data
        })
        that.setQuestion(that.data.level - 1)
      }
    })
  },

  // 设置题目
  setQuestion: function (index) {
    var that = this
    var data = that.data.quesArr[index]
    that.setData({
      questionTitle: data.question,
      answer: data.answer,
      A: data['A'],
      B: data['B'],
      C: data['C'],
      D: data['D']
    })
  },
})